package com.example.demo.controller;


import com.example.demo.config.ResponseModel;
import com.example.demo.entity.Cart;
import com.example.demo.entity.OrderItem;
import com.example.demo.exception.BusinessException;
import com.example.demo.service.CartService;
import com.example.demo.service.MenuService;
import com.example.demo.service.OrderItemService;
import com.example.demo.service.OrderService;
import com.example.demo.service.model.OrderModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "订单controller",tags = "订单操作接口")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;


    /**
     * @Author chenqiuran
     * @Description //TODO 根据订单ID获取订单信息
     * @Date 10:09 2021/8/29
     * @Param [Id]
     * @return com.example.demo.config.ResponseModel<com.example.demo.service.model.OrderModel>
     **/
    @ApiOperation(value="根据订单Id获取订单",tags={"获取订单信息"},httpMethod = "GET")
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ApiImplicitParam(name="Id",value="订单id",dataType="Long", paramType = "query")
    public ResponseModel<OrderModel> findById(Long Id) throws BusinessException {
        OrderModel orderModel = orderService.findById(Id);
        return new ResponseModel<>(orderModel, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 根据用户ID获取用户所有订单信息
     * @Date 10:09 2021/8/29
     * @Param [UserId, page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findAllOrderByUserId'")
    @ApiOperation(value = "根据用户Id获取订单",tags = {"获取订单信息"},httpMethod = "GET")
    @RequestMapping(value = "/findAllOrderByUserId",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "UserId",value = "用户Id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderModel>> findAllOrderByUserId(@RequestParam(value = "UserId") Long UserId,
                                                                @RequestParam(value = "page") Integer page,
                                                                @RequestParam(value = "limit") Integer limit){
        List<OrderModel> orderModelList = orderService.findAllOrderByUserIdWithRange(UserId,(page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 根据订单Id获取所有订单项
     * @Date 10:09 2021/8/29
     * @Param [orderId, page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.entity.OrderItem>>
     **/
    //@Cacheable(cacheNames = "orderItem", key = "'findOrderItemList'")
    @RequestMapping(value = "/findOrderItemList",method = RequestMethod.GET)
    @ApiOperation(value = "根据订单Id获取订单项",tags = {"获取订单项信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "OrderId",value = "订单Id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderItem>> findOrderItemList(@RequestParam(value = "OrderId") Long orderId,
                                                            @RequestParam(value = "page") Integer page,
                                                            @RequestParam(value = "limit") Integer limit){
        List<OrderItem> orderItemList = orderItemService.findAllOrderItemByOrderId(orderId,(page-1)*limit,limit);
        return new ResponseModel<>(orderItemList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 根据菜品名获取所有用户所有订单
     * @Date 10:10 2021/8/29
     * @Param [UserId, MenuName, page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findUserOrderByMenuName'")
    @RequestMapping(value = "/findUserOrderByMenuName",method = RequestMethod.GET)
    @ApiOperation(value = "根据用户Id和菜品名获取订单",tags = {"获取订单信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "menuName",value = "菜品名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderModel>> findUserOrderByMenuName(@RequestParam(value = "userId") Long userId,
                                                                   @RequestParam(value = "menuName") String menuName,
                                                                   @RequestParam(value = "page") Integer page,
                                                                   @RequestParam(value = "limit") Integer limit){
        List<OrderModel> orderModelList = orderService.findUserOrderByMenuName(userId,menuName,(page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 根据订单Id逻辑删除订单
     * @Date 10:10 2021/8/29
     * @Param [userId, orderId]
     * @return com.example.demo.config.ResponseModel<java.lang.Boolean>
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @ResponseBody
    @PutMapping("/deleteOrder")
    @ApiOperation(value = "根据订单Id删除订单",tags = {"删除订单"},httpMethod = "PUT")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId",value = "订单Id",dataType = "Long",paramType = "query")})
    public ResponseModel<Boolean> deleteOrder(@RequestParam(value = "orderId") Long orderId) throws BusinessException {
        Boolean result = orderService.deleteOrder(orderId);
        return new ResponseModel<>(result, "删除成功", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 创建订单
     * @Date 10:11 2021/8/29
     * @Param [orderModel]
     * @return com.example.demo.config.ResponseModel<java.lang.Object>
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @ResponseBody
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    @ApiOperation(value = "使用订单模型创建订单",tags = {"创建订单"},httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderModel",value = "订单模型",dataType = "OrderModel",paramType = "body")})
    public ResponseModel<Object> createOrder(@RequestBody OrderModel orderModel) throws BusinessException {
        orderService.createOrder(orderModel);
        return new ResponseModel<>(null, "创建成功", "200");
    }
    /**
     * @Author chenqiuran
     * @Description //TODO 确认订单
     * @Date 10:11 2021/8/29
     * @Param [userId, orderId]
     * @return com.example.demo.config.ResponseModel<java.lang.Object>
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @ResponseBody
    @PutMapping(value = "/confirmOrder")
    @ApiOperation(value = "根据订单Id确认订单",tags = {"确认订单"},httpMethod = "PUT")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId",value = "订单Id",dataType = "Long",paramType = "query")})
    public ResponseModel<Object> confirmOrder(@RequestParam(value = "orderId") Long orderId) throws BusinessException {
        Boolean yn = orderService.confirmOrder(orderId);
        logger.info("确认{}号订单完成",orderId);
        return new ResponseModel<>(null, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 确认已发货
     * @Date 15:56 2021/8/30
     * @Param [orderId]
     * @return com.example.demo.config.ResponseModel<java.lang.Object>
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @ResponseBody
    @PutMapping(value = "/deliverOrder")
    @ApiOperation(value = "根据订单Id确认已发货",tags = {"确认已发货"},httpMethod = "PUT")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId",value = "订单Id",dataType = "Long",paramType = "query")})
    public ResponseModel<Object> deliverOrder(@RequestParam(value = "orderId") Long orderId) throws BusinessException {
        Boolean yn = orderService.deliverOrder(orderId);
        logger.info("确认{}号订单已发货",orderId);
        return new ResponseModel<>(null, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 管理员获取所有订单
     * @Date 10:11 2021/8/29
     * @Param [page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findallorder'")//读取缓存
   // @CachePut(cacheNames = "order", key = "'findallorder'")
    @RequestMapping(value = "/admin/findAllOrder",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有订单",tags = {"获取订单信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "页码",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "int",paramType = "query")})
    public ResponseModel<List<OrderModel>> findAllOrder(@RequestParam(value = "page") Integer page,
                                                   @RequestParam(value = "limit") Integer limit){
        List<OrderModel> orderModelList = orderService.findAllOrder((page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 管理员根据菜品名查询订单
     * @Date 10:55 2021/8/29
     * @Param [menuName, index, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findAllByMenuName'")
    @RequestMapping(value = "/admin/findAllByMenuName",method = RequestMethod.GET)
    @ApiOperation(value = "根据菜品名获取订单",tags = {"获取订单信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuName",value = "菜品名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderModel>> findAllByMenuName(@RequestParam(value = "menuName")String menuName,
                                                             @RequestParam(value = "page")Integer page,
                                                             @RequestParam(value = "limit")Integer limit){
        List<OrderModel> orderModelList = orderService.findAllByMenuName(menuName,(page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 管理员根据电话号码查询订单
     * @Date 13:46 2021/8/29
     * @Param [telephone, page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findAllByTelephone'")
    @RequestMapping(value = "/admin/findAllByTelephone",method = RequestMethod.GET)
    @ApiOperation(value = "根据电话号码获取订单",tags = {"获取订单信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "telephone",value = "电话号码",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderModel>> findAllByTelephone(@RequestParam(value = "telephone")String telephone,
                                                              @RequestParam(value = "page")Integer page,
                                                              @RequestParam(value = "limit")Integer limit){
        List<OrderModel> orderModelList = orderService.findAllByTelephone(telephone,(page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 管理员根据用户名查询订单
     * @Date 13:47 2021/8/29
     * @Param [userName, page, limit]
     * @return com.example.demo.config.ResponseModel<java.util.List<com.example.demo.service.model.OrderModel>>
     **/
    //@Cacheable(cacheNames = "order", key = "'findAllByUserName'")
    //@CachePut(cacheNames = "order", key = "'findAllByUserName'")
    @RequestMapping(value = "/admin/findAllByUserName",method = RequestMethod.GET)
    @ApiOperation(value = "根据用户名获取订单",tags = {"获取订单信息"},httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userName",value = "用户名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "一页的订单数",dataType = "Integer",paramType = "query")})
    public ResponseModel<List<OrderModel>> findAllByUserName(@RequestParam(value = "userName")String userName,
                                                             @RequestParam(value = "page")Integer page,
                                                             @RequestParam(value = "limit")Integer limit){
        List<OrderModel> orderModelList = orderService.findAllByUserName(userName,(page-1)*limit,limit);
        return new ResponseModel<>(orderModelList, "success", "200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO 为订单付款
     * @Date 21:59 2021/8/31
     * @Param [orderId, userId]
     * @return com.example.demo.config.ResponseModel
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @RequestMapping(value = "/payForOrder",method = RequestMethod.PUT)
    @ApiOperation(value = "为订单付款",tags = {"为订单付款"},httpMethod = "PUT")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId",value = "订单Id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long",paramType = "query")})
    public ResponseModel payForOrder(@RequestParam(value = "orderId") Long orderId,
                                     @RequestParam(value = "userId") Long userId) throws BusinessException {
        orderService.payForOrder(orderId,userId);
        return new ResponseModel<>(null,"付款成功","200");
    }

    /**
     * @Author chenqiuran
     * @Description //TODO  使用购物车创建订单
     * @Date 22:11 2021/8/31
     * @Param [cartList, userId, notes]
     * @return com.example.demo.config.ResponseModel<java.lang.Object>
     **/
    //@CacheEvict(cacheNames = "order", key = "'findallorder'")//清除缓存
    @ResponseBody
    @RequestMapping(value = "/createOrderByCart", method = RequestMethod.POST)
    @ApiOperation(value = "使用购物车创建订单",tags = {"创建订单"},httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "notes",value = "备注",dataType = "String",paramType = "query")})
    public ResponseModel<Object> createOrderByCart(@RequestParam("userId") Long userId,
                                                   @RequestParam("notes") String notes) throws BusinessException {
        orderService.createOrderByCart(userId,notes);
        return new ResponseModel<>(null, "创建成功", "200");
    }


}
